package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.ContextDao;
import com.rstrulak.model.Context;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;

@Component("contextListBean")
@Scope("request")
public class ContextListBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6404062512030914853L;
	
	@Autowired
	private ContextDao contextDao;

	private List<Context> contextList = null;
	
	@PostConstruct
	public void init() {
		getContextListFromDB();
	}

	private void getContextListFromDB() {
		contextList = contextDao.getAll();
	}

	public List<Context> getContextList() {
		return contextList;
	}

	public void setContextList(List<Context> contextList) {
		this.contextList = contextList;
	}
	
	public void showContextDetails(Long contextId) throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.CONTEXT_ID, contextId);
		JSFUtils.redirect("/protected/contextDetails.xhtml");
	}
	
	public void goToCreateContextSite() throws ServletException, IOException {
		JSFUtils.redirect("/protected/newContext.xhtml");
	}
	
	public void deleteContext(Context context) throws ServletException, IOException{
		contextDao.delete(context);
		JSFUtils.redirect("/protected/context.xhtml");
		
	}
	
}
